package m.toolkit.codegenerator;

import java.util.ArrayList;
import java.util.List;

import m.framework.ClassType;
import m.framework.controller.Task;
import m.framework.dao.mapping.BeanField;
import m.framework.dao.mapping.BeanInfo;
import m.framework.exception.SystemException;

public class ModelConfig {
	// 系统字段,新增和修改都不能操作的
	public static final String[] SYSTEM_FILEID = new String[] { "createTime", "createId" };
	private BeanInfo beanInfo;
	private Class controlerClass;

	public ModelConfig(Class controlerClass) {
		Class modelClass = ClassType.controller.getOrtherType(controlerClass, ClassType.model);
		this.beanInfo = BeanInfo.getBeanInfo(modelClass);
		this.controlerClass = controlerClass;
	}

	public String getShowName() {
		Task tk = (Task) controlerClass.getAnnotation(Task.class);
		return tk.value().split(",")[0];
	}

	public String view(BeanField field) {
		return this.showInList(field);
	}


	public String getModelName() {
		return beanInfo.getModelName();
	}

	public BeanField pk() {
		return beanInfo.getPk();
	}

	public List<BeanField> showList() {
		return beanInfo.getDbFieldWithoutPk();
	}

	public String showInList(BeanField field) {
		return "$" + beanInfo.getModelName() + "." + field.getName() + "HtmlTable";
	}

	public List<List<BeanField>> split(List<BeanField> fields, int number) {
		List result = new ArrayList();
		List temp = new ArrayList();
		int no = 0;
		for (BeanField bf : fields) {
			no++;
			temp.add(bf);
			if (number == no) {
				result.add(temp);
				temp = new ArrayList();
				no = 0;
			}
		}
		if (temp.size() > 0) {
			result.add(temp);
		}
		return result;
	}

	public List<List<BeanField>> queryField(int number) {
		return split(beanInfo.getDbFieldWithoutPk(), number);
	}

	/**只允许新增除了主键以外的dto字段*/
	public List<List<BeanField>> insertField(int number) {
		List<BeanField> all = beanInfo.getDbFieldWithoutPk();
		List list = new ArrayList();
		for (BeanField bf : all) {
			if (bf.getDeclaringClass().getName().endsWith("Dto")) {
				list.add(bf);
			}
		}
		return split(list, number);
	}

	public String getTableValue(){
		return beanInfo.table().value();
	}
	
	public String getTableValue(String pix){
		return pix+beanInfo.table().value();
	}
	
	public String doller(){
		return "$";
	}
	
	
	
	
	public List<List<BeanField>> updateField(int number) {
		return insertField(number);
	}
	
	public String lowerCaseName(){
		return beanInfo.getModelName().toLowerCase();
	}

	public List<List<BeanField>> viewField(int number) {
		return split(beanInfo.getDbFieldWithoutPk(), number);
	}

	public String show(BeanField field) {
		return "$model." + field.getName();
	}

	public String getDollar() {
		return "$";
	}
}
